package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson2.JSONObject;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackOrderVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;

    // 上传音频文件
    // @GuiguLogin 前端没有配置请求token 所以不设置
    @Operation(summary = "上传音频文件")
    @PostMapping("uploadTrack")
    public Result uploadTrackFile(@RequestParam("file") MultipartFile file) {

        // 后端需要响应的结果
        // 按照前端的要求封装数据并返回 mediaFileId  mediaUrl

        JSONObject jsonObject = vodService.uploadTrackFile(file);
        return Result.ok(jsonObject);
    }

    // 保存声音
    @GuiguLogin
    @Operation(summary = "保存声音")
    @PostMapping("saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfo trackInfo) {

        // 获取前端传来的参数
        // albumId: 1618
        //coverUrl: "http://192.168.100.100:9000/tingshu/2025/04-06/1908731484198535168.jpg"
        //isOpen: 1
        //mediaFileId: "1397757909036418213"
        //mediaUrl: "https://1500037587.vod-qcloud.com/6cd1a746vodcq1500037587/a3d041aa1397757909036418213/tLJhHLy4nccA.mp3"
        //trackIntro: "第一次上传"
        //trackTitle: "first"
        trackInfoService.saveTrackInfo(trackInfo);
        return Result.ok();
    }

    // http://127.0.0.1:8500/api/album/trackInfo/findUserTrackPage/1/10
    @GuiguLogin
    @Operation(summary = "分页查询声音列表")
    @PostMapping("findUserTrackPage/{pageNum}/{pageSize}")
    public Result findUserTrackPage(@PathVariable Long pageNum,
                                    @PathVariable Long pageSize,
                                    TrackInfoQuery trackInfoQuery) {
        Page<TrackListVo> page = trackInfoService.findUserTrackPage(pageNum, pageSize, trackInfoQuery);

        return Result.ok(page);
    }

    // http://127.0.0.1:8500/api/album/trackInfo/removeTrackInfo/51946
    @GuiguLogin
    @Operation(summary = "删除声音")
    @DeleteMapping("removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }

    // http://127.0.0.1:8500/api/album/trackInfo/getTrackInfo/51948
    @GuiguLogin
    @Operation(summary = "获取声音信息")
    @GetMapping("getTrackInfo/{id}")
    public Result getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    // http://127.0.0.1:8500/api/album/trackInfo/updateTrackInfo/51948
    @GuiguLogin
    @Operation(summary = "修改声音信息")
    @PutMapping("updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id,
                                  @RequestBody TrackInfo trackInfo) {
        trackInfo.setId(id);
        trackInfoService.updateTrackInfo(id, trackInfo);
        return Result.ok();
    }

    // http://127.0.0.1:8500/api/album/trackInfo/findAlbumTrackPage/680/1/10
    // 根据专辑id查询声音的分页列表数据 // 并设置收费标识
    @GuiguLogin(required = false)
    @Operation(summary = "根据专辑id查询声音的分页列表数据")
    @GetMapping("findAlbumTrackPage/{albumId}/{pageNum}/{pageSize}")
    public Result findAlbumTrackPage(
            @PathVariable("albumId") Long albumId,
            @PathVariable("pageNum") Long pageNum,
            @PathVariable("pageSize") Long pageSize
    ) {
        Page<AlbumTrackListVo> page = trackInfoService.findAlbumTrackPage(albumId, pageNum, pageSize);
        return Result.ok(page);
    }
    // 获取用户声音购买支付列表  购买声音的方式 比如 买一集 后5级 全部等等
    // http://127.0.0.1:8500/api/album/trackInfo/findUserTrackPaidList/36615
    @GuiguLogin
    @Operation(summary = "获取用户声音购买支付方式的列表")
    @GetMapping("findUserTrackPaidList/{id}")
    public Result findUserTrackPaidList(@PathVariable("id") Long id) {

        // List<JSONobject>  或者 list<Map>
        // [
        //      {'name':'本级','price':30,'trackCount':0}
        //      {'name':'后5级','price':30,'trackCount':5}
        //      {'name':'后10级','price':30,'trackCount':10}
        //      {'name':'后1所有级','price':30,'trackCount':级数}
        // ]
        // 通过声音id可以查询专辑id进而查询专辑信息
        List<TrackOrderVo> list = trackInfoService.findUserTrackPaidList(id);
        return Result.ok(list);
    }

    // 查询声音的统计信息
    // http://127.0.0.1:8500/api/album/trackInfo/getTrackStatVo/51946
    @Operation(summary = "获取声音的统计信息")
    @GetMapping("getTrackStatVo/{id}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long id) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(id);
        return Result.ok(trackStatVo);
    }





    // 远程调用根据id获取声音的基本信息
    // api/album/trackInfo/getTrackInfo/{id}
    @Operation(summary = "获取声音信息")
    @GetMapping("getTrackInfoInner/{id}")
    public Result<TrackInfo> getTrackInfoInner(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }
    // 远程调用获取需要购买的专辑声音
    // api/album/trackInfo/getNeedTrackInfoList/{albumId}/{trackNo}/{count}
    @Operation(summary = "获取需要购买的专辑声音列表")
    @GetMapping("getNeedTrackInfoList/{albumId}/{trackNo}/{count}")
    public Result<List<TrackInfo>> getNeedTrackInfoList(@PathVariable Long albumId,
                                       @PathVariable Long trackNo,
                                       @PathVariable Integer count) {
        List<TrackInfo> list = trackInfoService.getNeedTrackInfoList(albumId, trackNo, count);
        return Result.ok(list);
    }
}

